package com.jy.it.ui.cus;

import java.util.List;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.jy.it.entity.User;
import com.jy.it.entity.Wz;
import com.jy.it.service.UserService;
import com.jy.it.service.WzService;

@Controller
public class WzCusController {
	@Autowired
	private WzService service;
	@Autowired
	private UserService userService;
	
	@RequestMapping(value="/cus/wz/list",method=RequestMethod.GET,params= {})
	public String showList() {
		return "cus/wz_list";
	}
	
	@RequestMapping(value="/cus/wz/list",method=RequestMethod.GET,params= {"carno"})
	public String showList(String carno,Model m) {
		List<Wz> wzList=service.findWzByCarNo(carno);
		m.addAttribute("wzlist",wzList);
		return "cus/wz_list";
	}
	
	@RequestMapping(value="/cus/wz/list",method=RequestMethod.GET,params= {"carid"})
	public String showList(int carid,Model m) {
		List<Wz> wzList=service.findWzByCar(carid);
		m.addAttribute("wzlist",wzList);
		return "cus/wz_list";
	}
	
	@RequestMapping(value="/cus/wz/detail",method=RequestMethod.GET)
	public String showOne(int wzid,Model m) {
		Wz wz=service.findById(wzid);
		m.addAttribute("wz",wz);
		return "cus/wz_detail";
	}
	
	@RequestMapping(value="/cus/wz/confirm",method=RequestMethod.GET)
	public String showConfirm(int wzid,Model m) {
		Wz wz=service.findById(wzid);
		m.addAttribute("wz",wz);
		return "cus/wz_confirm";
	}
	
	@RequestMapping(value="/cus/wz/pay",method=RequestMethod.GET)
	public String pay(int wzid,HttpSession session) {
		User u=(User)session.getAttribute("user");//当前用户
		Wz wz=service.findById(wzid);//根据id查出罚单对象
		//支付罚款（传入按个罚单，哪个人确认支付）
		int n=service.pay(wz,u.getId());
		//支付完成户，当前用户的分数有变化；需要更新一下session里面的user，否则还会显示旧的分数
		session.setAttribute("user",userService.findById(u.getId()));//重新加载一下session当前用户（分数变化）
		if(n==1)
			return "redirect:/cus/wz/list?carid="+wz.getCarId();//支付成功，跳转到罚单列表，观察状态变化
		else
			return "redirect:/cus/wz/confirm?err=1&wzid="+wzid;//支付不成功，则跳回确认界面
	}

}
